$(function(){
    // 商家切换
    $(".shop_type").on('change',function () {
        $val = $(this).val();
        if($val == 0){
            $('#shop_enterprise').removeClass('show').addClass('hidden');
            $('#shop_person').removeClass('hidden').addClass('show');
        }else{
            $('#shop_person').removeClass('show').addClass('hidden');
            $('#shop_enterprise').removeClass('hidden').addClass('show');
        }
    })

    uploader.eventBind();

    $(".btn-last-save").on("click",function(){
        var shop_type=$(".shop_type").val();

        var flag1=true;
        if(shop_type=="0"){
            var tree_id=$('.classify option:selected').attr("data-id");
            var classify=$('.classify option:selected').val();
            var shop_name=$(".shop-name").val();
            var shop_account=$(".shop-account").val();
            var shop_pswd=$(".shop-pswd").val();
            var shop_user_name=$(".shop-user-name").val();
            var shop_email=$(".shop-email").val();
            var shop_phone=$(".shop-phone").val();
            // var shop_join_name=$(".shop-join-name").val();
            // var shop_join_email=$(".shop-join-email").val();
            var shop_json_phone_two=$(".shop-join-phone-two").val();
            // var shop_join_phone=$(".shop-join-phone").val();
            var shop_card_idcard=$(".shop-card-idcard").val();
            var hidden_pic1=$(".hidden-pic1").val();
            var hidden_pic2=$(".hidden-pic2").val();
            var hidden_pic3=$(".hidden-pic3").val();

            if(shop_name==""){
                layer.msg("请填写商店名称");
                flag1=false;
            }else if(shop_account==""){
                layer.msg("请填写登录账号");
                flag1=false;
            }else if(shop_user_name==""){
                layer.msg("请填写店铺管理人姓名");
                flag1=false;
            }else if(shop_email==""){
                layer.msg("请填写店铺管理人邮箱");
                flag1=false;
            }else if(shop_phone==""){
                layer.msg("请填写店铺管理人手机号");
                flag1=false;
            // }else if(shop_join_name==""){
            //     layer.msg("请填写入住人姓名");
            //     flag1=false;
            // }else if(shop_join_email==""){
            //     layer.msg("请填写入住人邮箱");
            //     flag1=false;
            // }else if(shop_join_phone==""){
            //     layer.msg("请填写入住人手机号");
            //     flag1=false;
            }else if(shop_card_idcard==""){
                layer.msg("请填写店铺管理人身份证号");
                flag1=false;
            }else if(hidden_pic1==""){
                layer.msg("请添加店铺管理人身份证正面照");
                flag1=false;
            // }else if(hidden_pic3==""){
            //     layer.msg("请添加店铺管理人身份证反面照");
            //     flag1=false;
            }else if(hidden_pic3==""){
                layer.msg("请添加店铺管理人手持身份证照");
                flag1=false;
            };
            var form1=$(".form1").serialize();

            var dataStr1="type=0&tree_id="+tree_id+"&"+form1;
            ajax("post","/shop/update-shop",dataStr1,function success(data){
                layer.msg(data.msg);
                setTimeout(function(){
                    location.href="/shop/index";
                },1000);
            },function error(data){
                layer.msg(data.msg);
            })
        }else{
            var tree_id=$('.classify1 option:selected').attr("data-id");
            var classify1=$('.classify1 option:selected').val();
            var shop_name1=$(".shop-name1").val();
            var shop_account1=$(".shop-account1").val();
            var shop_pswd1=$(".shop-pswd1").val();
            var shop_user_name1=$(".shop-user-name1").val();
            var shop_email1=$(".shop-email1").val();
            var shop_phone1=$(".shop-phone1").val();
            var join_name=$(".join-name").val();
            var join_phone=$(".join-phone").val();
            var join_num=$(".join-num").val();
            var hidden_pic4=$(".hidden-pic4").val();
            var hidden_pic5=$(".hidden-pic5").val();
            var hidden_pic6=$(".hidden-pic6").val();
            var compy_name=$(".compy-name").val();
            var province=$('.m-wrap1 option:selected').val();
            var city=$('.m-wrap2 option:selected').val();
            var country=$('.m-wrap3 option:selected').val();
            var addr=$(".addr").val();
            var compy_num=$(".compy-num").val();
            var hidden_pic7=$(".hidden-pic7").val();
            var hidden_pic8=$(".hidden-pic8").val();


            if(shop_name1==""){
                layer.msg("请填写商店名称");
                flag1=false;
            }else if(shop_account1==""){
                layer.msg("请填写登录账号");
                flag1=false;
            }else if(shop_user_name1==""){
                layer.msg("请填写店铺管理人姓名");
                flag1=false;
            }else if(shop_email1==""){
                layer.msg("请填写店铺管理人邮箱");
                flag1=false;
            }else if(shop_phone1==""){
                layer.msg("请填写店铺管理人手机号");
                flag1=false;
            }else if(join_name==""){
                layer.msg("请填写法人代表姓名");
                flag1=false;
            }else if(join_phone==""){
                layer.msg("请填写法人代表手机号");
                flag1=false;
            }else if(join_num==""){
                layer.msg("请填写法人代表身份证");
                flag1=false;
            }else if(hidden_pic4==""){
                layer.msg("请添加法人代表身份证正面照");
                flag1=false;
            }else if(hidden_pic5==""){
                layer.msg("请添加法人代表身份证反面照");
                flag1=false;
            }else if(hidden_pic6==""){
                layer.msg("请添加法人代表手持身份证照片");
                flag1=false;
            }else if(compy_name==""){
                layer.msg("请填写公司名称");
                flag1=false;
            }else if(province==""){
                layer.msg("请选择省");
                flag1=false;
            }else if(city==""){
                layer.msg("请选择市");
                flag1=false;
            }else if(country==""){
                layer.msg("请选择区");
                flag1=false;
            }else if(addr==""){
                layer.msg("请填写详细地址");
                flag1=false;
            }else if(compy_num==""){
                layer.msg("请填写统一社会信用代码");
                flag1=false;
            }else if(hidden_pic7==""){
                layer.msg("请添加营业执照");
                flag1=false;
            }else if(hidden_pic8==""){
                layer.msg("请添加开户许可证");
                flag1=false;
            };
            var form2=$(".form2").serialize();
            var dataStr2="type=1&tree_id="+tree_id+"&"+form2;  
            ajax("post","/shop/update-shop",dataStr2,function success(data){
                if (data.code != 200) {
                    layer.msg(data.msg);
                } else {
                    setTimeout(function(){
                        location.href="/shop/index";
                    },1000);
                }
            },function error(data){
                layer.msg(data.msg);
            })         
        }
    });

    $('.enlarge-picture').on('click',function(){
        var img_src=$(this).parents(".control-group").find("img").attr("src");
        $(".large_pic").show();
        $(".large_pic").find("img").attr("src",img_src);
    })

    $(".xxx").on("click",function(){
        $(".large_pic").hide();
    })
});

var uploader = {
    'data':{
        'token':'',
        'uploadDomain':'',
        'domain':''
    },
    'complete':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic1").attr("src",url);
        $(".hidden-pic1").val(res.key);
    },
    'complete1':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic2").attr("src",url);
        $(".hidden-pic2").val(res.key);
    },
    'complete2':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic3").attr("src",url);
        $(".hidden-pic3").val(res.key);
    },
    'complete3':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic4").attr("src",url);
        $(".hidden-pic4").val(res.key);
    },
    'complete4':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic5").attr("src",url);
        $(".hidden-pic5").val(res.key);
    },
    'complete5':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic6").attr("src",url);
        $(".hidden-pic6").val(res.key);
    },
    'complete6':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic7").attr("src",url);
        $(".hidden-pic7").val(res.key);
    },
    'complete7':function(res){
        var url="http://"+uploader.data.domain +"/"+ res.key;
        $(".pic8").attr("src",url);
        $(".hidden-pic8").val(res.key);
    },
    'error':function () {
        console.dir(arguments);
    },
    // 分片上传时的进度展示功能在此书写.
    'next':function (response) {
        // var chunks = response.chunks || [];
        // var total = response.total;
    },
    'init':function(){
        $.ajax({
            'url':'/upload/get-token',
            'type':'POST',
            'dataType':'json',
            'success':function(data){
                if(data.code != 200)
                {
                    layer.alert(data.msg);
                    return false;
                }
                uploader.data.token = data.data.token;      // token
                uploader.data.domain = data.data.domain;    // 显示域名
                // uploader.data.uploadDomain = 'idv093d.qiniudns.com';    // 上传域名
                // console.log(uploader.data.token)
            }
        });
    },
    'eventBind':function(){
        this.init();
        $('.pic-card1').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(this)[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
        $('.pic-card2').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(this)[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete1
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
        $('.pic-card3').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(this)[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete2
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
        $('.pic-card4').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(this)[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete3
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
        $('.pic-card5').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(this)[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete4
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
        $('.pic-card6').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(this)[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete5
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
        $('.pic-card7').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(this)[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete6
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
        $('.pic-card8').on('change',function () {
            var token = uploader.data.token;
            var config = {
                useCdnDomain: true,
                disableStatisticsReport: false,
                region: qiniu.region.z0
            };
            var putExtra = {
                fname: "",
                params: {},
                mimeType: ["image/png", "image/jpeg", "image/gif"], // 限制文件类型.
            };
            var file = this.files[0];
            var observable;
            if (file) {
                var files=$(this)[0].files[0];    // 用来显示的文件名.需要修改,重名时覆盖
                var key1=file.name;
                var key=key1.split(".")[0]+randomNum()+"."+key1.split(".")[1];
                putExtra.params["x:name"] = key.split(".")[0];
                // 配置完成的数组.
                var subObject = {
                    next: uploader.next,
                    error: uploader.error,
                    complete: uploader.complete7
                };
                var subscription;
                // 调用sdk上传接口获得相应的observable，控制上传和暂停
                observable = qiniu.upload(files, key, token, putExtra, config);
                subscription = observable.subscribe(subObject);
                // subscription.unsubscribe();  // 取消上传.
            }
        });
    }
};